package com.ai.networking.netty.client;


import com.ai.common.constant.PlcConstants;
import com.ai.entity.network.IdentifySysToL1Message;
import com.ai.entity.network.IdentifyToPLCHeartMessage;
import com.ai.networking.netty.comm.CommonNetty;
import com.ai.entity.comm.SysNetty;
import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 客户端与服务端的心跳
 */
@Slf4j
@Component
@Configuration
@EnableScheduling
public class BootNettyHeartTimer {

    //  使用定时器发送心跳
    @Scheduled(cron = "0/30 * * * * ?")
    public void heart_timer() {
        // 心跳自定义
        String back = "heart";


        // 所有已经连接的客户端
        if (CommonNetty.NATIVE_CLIENT_MAP.size() > 0) {
            //所有需要并且已经连接的客户端
            for (Map.Entry<String, SysNetty> entry : CommonNetty.NATIVE_CLIENT_MAP.entrySet()) {
                String code = entry.getKey();
                SysNetty sysNetty = entry.getValue();
                // 判断是否创建成功   ----------   通过code来判断对应的功能是哪个
                if (BootNettyClientChannelCache.channelMapCache.containsKey(code)) {
                    // 创建成功
                    BootNettyClientChannel bootNettyChannel = BootNettyClientChannelCache.channelMapCache.get(code);
                    if (bootNettyChannel != null && bootNettyChannel.getChannel().isOpen()) {
                        // : 2023-06-20 修改心跳信号内容----不同的端口 发送的心跳内容不同
                        String heartJson = createHeartJson(code);
                        bootNettyChannel.getChannel().writeAndFlush(Unpooled.copiedBuffer(heartJson, CharsetUtil.UTF_8));
//                        bootNettyChannel.getChannel().writeAndFlush(Unpooled.buffer().writeBytes(back.getBytes()));
                    }
                } else {
                    log.info(code + "创建失败,准备重连...");
                    new BootNettyClientThread(sysNetty).start();
                }
            }
        }
    }

    /**
     * 不同的nettyCode返回不同的心跳电文
     * @param plcCode
     * @return
     */
    public  String createHeartJson(String plcCode){
        String telId="";
        if (plcCode.equals(PlcConstants.PLC_1_CODE)){
            telId=PlcConstants.PLC_1_HEART_TEL_ID;
        }else if (plcCode.equals(PlcConstants.PLC_2_CODE)){
            telId=PlcConstants.PLC_2_HEART_TEL_ID;
        }else {
            //plc_3
            telId=PlcConstants.PLC_3_HEART_TEL_ID;
        }

        //心跳报文
        IdentifyToPLCHeartMessage identifyToPLCHeartMessage = new IdentifyToPLCHeartMessage();
        identifyToPLCHeartMessage.setTelId(telId);
        identifyToPLCHeartMessage.setCounter(1);
        String jsonString = JSONObject.toJSONString(identifyToPLCHeartMessage);
        int length = jsonString.getBytes().length;
        identifyToPLCHeartMessage.setTelLen(length);

        //心跳报文
        String heartJson = JSONObject.toJSONString(identifyToPLCHeartMessage);
        return heartJson;

    }

}